package com.stone.framework.basic.dict.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stone.framework.basic.dict.service.DictMemberService;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.basic.dict.DictMemberPageReqBody;
import com.stone.framework.message.basic.dict.DictMemberPageResBody;
import com.stone.framework.message.basic.dict.DictMemberReqBody;
import com.stone.framework.message.basic.dict.DictMemberResBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "数据字典项")
@RestController
@RequestMapping("/dict/member")
public class DictMemberController {

    @Autowired
    private DictMemberService memberService;

    @ApiOperation(value = "分页查询", httpMethod = "POST")
    @PostMapping("/getPage")
    public PageResMessage<DictMemberPageResBody>
        getPage(@Valid @RequestBody PageReqMessage<DictMemberPageReqBody> reqMessage) {
        Page<DictMemberPageResBody> page =
            memberService.queryPage(reqMessage.getBody().getDictCategoryCode(), reqMessage.buildPageable());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "键值对查询", httpMethod = "POST")
    @PostMapping("/getKV")
    public ResMessage<Map<String, String>> getKV(@Valid @RequestBody ReqMessage<String> reqMessage) {
        Map<String, String> map = memberService.queryKVByCategoryCode(reqMessage.getBody());
        return PageResMessage.success(map);
    }

    @ApiOperation(value = "查询", httpMethod = "POST")
    @PostMapping("/getList")
    public ResMessage<List<DictMemberResBody>> getList(@Valid @RequestBody ReqMessage<String> reqMessage) {
        List<DictMemberResBody> list = memberService.queryByCategoryCode(reqMessage.getBody());
        return PageResMessage.success(list);
    }

    @ApiOperation(value = "查询（前端展示）", httpMethod = "POST")
    @PostMapping("/getDisplayList")
    public ResMessage<List<DictMemberResBody>> getDisplayList(@Valid @RequestBody ReqMessage<String> reqMessage) {
        List<DictMemberResBody> list = memberService.queryByCategoryCode(reqMessage.getBody()).parallelStream()
            .filter(res -> !res.hidden()).collect(Collectors.toList());
        return PageResMessage.success(list);
    }

    @ApiOperation(value = "保存", httpMethod = "POST")
    @PostMapping(value = "/save")
    ResMessage<Void> save(@Valid @RequestBody ReqMessage<List<DictMemberReqBody>> reqMessage) {
        memberService.save(reqMessage.getBody());
        return ResMessage.success();
    }

}
